package com.xtjc03.serial.service;

import com.xtjc03.jms.service.JmsServiceImpl;
import com.xtjc03.serial.domain.ComProperties;
import com.xtjc03.serial.domain.MySerialPort;
import org.apache.activemq.command.ActiveMQTextMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import javax.jms.JMSException;
import java.util.concurrent.CompletableFuture;

@Service
public class SerialPortServiceImpl implements SerialPortService{

    @Autowired
    MySerialPort serialPort;

    @Autowired
    JmsServiceImpl jmsService;

    @Autowired
    ComProperties comProperties;

    @Override
    public String  sendStringToCom(String msg) {
        return sendStringToCom(comProperties.getListener(), msg);
    }

    @Override
    public String sendStringToCom(String destination, String msg) {
        serialPort.getIoSession().write(msg);
        ActiveMQTextMessage test = jmsService.addReceiver(destination);
        if (test != null){
            try {
                return test.getText();
            } catch (JMSException e) {
                e.printStackTrace();
            }
        }
        return null;
    }
}
